Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetch): allow subclass methods of Headers to be called correctly #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ArnoSaine
Copy link

This PR fixes an issue where subclass methods of Headers were not being called correctly.

Problem

When a class extends Headers, the subclass's methods (like append) were not accessible or called properly. This was caused by the Proxy instance in the Headers constructor, which intercepted method calls and directed them to the URLSearchParams prototype, bypassing any overrides defined in subclasses.

Solution

  • Removed the Proxy from the constructor.
  • Re-implemented the proxied methods in the Headers class to maintain the expected behavior and allowing overrides by subclasses.

Example

import SuperHeaders from "@mjackson/headers"; // Extends `Headers` in Remix

// Correctly initialized. The constructor calls a setter directly from `SuperHeaders.prototype`.
console.log(new SuperHeaders({ cookie: "foo=123" }).cookie.size); // 1

// Incorrectly initialized. The constructor calls `this.append`, which is unexpectedly not handled
// by the subclass due to the Proxy.
console.log(new SuperHeaders([["cookie", "foo=123"]]).cookie.size); // 0

cc: @mjackson

Copy link

changeset-bot bot commented Aug 30, 2024

🦋 Changeset detected

Latest commit: ff5d0ff

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@remix-run/web-fetch Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@MichaelDeBoey MichaelDeBoey changed the title fix: Allow subclass methods of Headers to be called correctly fix(fetch): allow subclass methods of Headers to be called correctly Apr 7, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants